package timing.ukulele.third.msgChannel.mq.producer;

import com.alibaba.fastjson2.JSON;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import timing.ukulele.third.constant.MessageMqConstants;
import timing.ukulele.third.msgChannel.model.SendTaskInfo;
import java.nio.charset.StandardCharsets;

/**
 * @Description mq消息发送
 * @Author zh
 * @Date 2025-04-03 17:22
 */
@Component
@Slf4j
@AllArgsConstructor
public class MessageRabbitMqProducer {

    private final RabbitTemplate rabbitTemplate;

    /**
     *  将代发渠道消息推送至MQ，实现异步以及削峰
     * @param sendTaskInfo
     */
    public void sendChannelMessageToMq(SendTaskInfo sendTaskInfo) {
        // 构建消息体
        Message message = MessageBuilder.withBody(JSON.toJSONString(sendTaskInfo).getBytes(StandardCharsets.UTF_8))
                .build();
        // 发送消息
        rabbitTemplate.convertAndSend(MessageMqConstants.MESSAGE_NORMAL_DIRECT_EXCHANGE_ONE,
                MessageMqConstants.MESSAGE_SEND_DIRECT_ROUTING_KEY, message);
    }

}
